#include "EditorPanelView.h"
#include "EditorPanelViewModel.h"

#include "Widgets/RectangleWidget.h"
#include "Widgets/EditorImageButton.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/Positioners/HorizontalPositionerWidget.h"

#include "Ability/EditorAbilityView.h"
#include "Ability/EditorAbilityViewModel.h"

#include "Object/NewObject.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

void REditorPanelView::BindViewModel(TSharedObjectPtr<REditorPanelViewModel> InViewModel)
{
    ViewModel=InViewModel;

    for(int64_t i=0;i<ViewModel->GetPanelAbilitysNum();++i)
    {
        auto AbilityViewModel=ViewModel->GetPanelAbility(i);
        auto ViewModelClass=AbilityViewModel->GetClass();
        auto ViewClass=REditor::Get()->GetViewClass(ViewModelClass);
        auto AbilityView=NewObject<REditorAbilityView>(ViewClass);
        AbilityView->BindViewModel(AbilityViewModel);
        PanelAbilitys.Add(AbilityView);
    }    

}

void REditorPanelView::FillWidget(TSharedObjectPtr<RWidget> InParentWidget)
{
    auto Root=NewWidget<RWidget>(InParentWidget);
    Root->Anchor.Fill(InParentWidget);

    auto Background=NewWidget<RRectangleWidget>(Root);
    Background->Anchor.Fill(Root);
    Background->FillColor=REditor::Get()->CurrentTheme->BackgroundColor;
    auto TitleBar=NewWidget<RWidget>(Root);
    TitleBar->Height=ViewModel->PanelTitleHeight;
    TitleBar->Anchor.HorizontalFill(Root);

    auto PanelButtons=NewWidget<RWidget>(TitleBar);
    {
        PanelButtons->Anchor.VerticalFill(TitleBar);
        PanelButtons->Anchor.RightReference=TitleBar;
        PanelButtons->Anchor.RightOffset=20.f;
        PanelButtons->Width=30;

        auto CloseButton=
            WHE::CreateImageButton(
                ViewModel->PanelCloseIcon,
                CString(U"隐藏面板"),
                [this](){
                    this->OnHidePanel.Execute();
                },
                PanelButtons
            );
        CloseButton->Anchor.VerticalCenterReference=PanelButtons;
    }

    auto NameBar=NewWidget<RHorizontalPositionerWidget>(TitleBar);
    NameBar->Anchor.VerticalFill(TitleBar);
    NameBar->Anchor.RightReference=PanelButtons;
    NameBar->Anchor.RightOffset=10;
    NameBar->Anchor.RightReferenceEdge=EHorizontalWidgetEdge::Left;
    NameBar->Anchor.LeftReference=TitleBar;
    NameBar->Anchor.LeftOffset=10;
    NameBar->Spacing=10;

    auto OperationsBar=NewWidget<RWidget>(TitleBar);
    OperationsBar->Anchor.Fill(NameBar);


    for(int64_t AbilityIndex=0; AbilityIndex<PanelAbilitys.Num(); ++AbilityIndex)
    {
        //fill names
        auto AbilityTitle=NewWidget<RWidget>(NameBar);
        AbilityTitle->Anchor.VerticalFill(NameBar);
        auto WidgetGetter=PanelAbilitys[AbilityIndex]->FillTextTitleBar(AbilityTitle);
        AbilityTitle->Width=WidgetGetter;
        
        auto TitleMouseWidget=NewWidget<RMouseWidget>(AbilityTitle);
        TitleMouseWidget->Anchor.Fill(AbilityTitle);
        TitleMouseWidget->OnClicked.BindLambda([this,AbilityIndex](const SMouseButtonEvent&){
            this->ViewModel->SetActivedPanelAbilityIndex(AbilityIndex);
            return true;
        });

        auto Highlight=NewWidget<RRectangleWidget>(AbilityTitle);
        Highlight->Anchor.BottomReference=AbilityTitle;
        Highlight->Height=1.f;
        Highlight->Anchor.HorizontalFill(AbilityTitle);
        Highlight->Visible=[this,AbilityIndex](){
            return this->ViewModel->IsPanelAbilityVisible(AbilityIndex);
        };
        Highlight->FillColor=ViewModel->PanelTitleHighlightColor;

        //fill operations
        auto AbilityOperations=NewWidget<RWidget>(OperationsBar);
        AbilityOperations->Anchor.Fill(OperationsBar);
        PanelAbilitys[AbilityIndex]->FillOperations(AbilityOperations);
        AbilityOperations->Visible=[this,AbilityIndex](){
            return this->ViewModel->IsPanelAbilityVisible(AbilityIndex);
        };
        

    }

    //create ability content
    auto AbilityContainer=NewWidget<RWidget>(Root);
    AbilityContainer->Anchor.TopReference=TitleBar;
    AbilityContainer->Anchor.TopReferenceEdge=EVerticalWidgetEdge::Bottom;
    AbilityContainer->Anchor.BottomReference=Root;
    AbilityContainer->Anchor.LeftReference=Root;
    AbilityContainer->Anchor.RightReference=Root;

    //AbilityContainer->bClipSubWidgets=true;

    for(int64_t Index=0; Index<PanelAbilitys.Num(); ++Index)
    {
        auto AbilityContent=NewWidget<RWidget>(AbilityContainer);
        AbilityContent->Anchor.Fill(AbilityContainer);
        AbilityContent->Visible=[this,Index](){
            return this->ViewModel->IsPanelAbilityVisible(Index);
        };
        PanelAbilitys[Index]->CreateWidget(AbilityContent);
    }
}